package com.wang.rabbitmq.topicmq;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * @author: wang
 * @date: 2021/8/3 18:54
 * @description:
 */
public class MyReceiveTopicMq {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUsername("root");
        factory.setPassword("root");
        factory.setPort(5672);
        factory.setHost("192.168.169.123");
        factory.setVirtualHost("/");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare("topicExchange01", BuiltinExchangeType.TOPIC);
        channel.queueDeclare("topicQueue1", true, false, false, null);
        channel.queueBind("topicQueue1", "topicExchange01", "a");

        channel.basicConsume("topicQueue1", true, new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(consumerTag);
                System.out.println(envelope.getRoutingKey());
                System.out.println(envelope.getExchange());
                System.out.println(envelope.getDeliveryTag());
                System.out.println(new String(body));
                System.out.println("===========================");
            }
        });
    }
}
